package com.edushare.activity;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.edushare.R;
import com.edushare.R.layout;
import com.edushare.R.menu;
import com.edushare.controller.CommentFunctions;
import com.edushare.controller.UserFunctions;
import com.edushare.entity.Comment;
import com.edushare.entity.Globals;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class DrawCommentActivity extends Activity {
	private Button commentBtn;
	private EditText commentTxt;
	private ListView commentItems;

	ArrayAdapter<String> commentAdaptor;
	ArrayList<Comment> commentList = new ArrayList<Comment>();
	ArrayList<String> commentStrList = new ArrayList<String>();

	HashMap<String, String> userDetails;
	String username;
	String userID;

	CommentFunctions commentFunction = new CommentFunctions();
	UserFunctions userFunction = new UserFunctions();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.draw_comment);

		commentItems = (ListView) findViewById(R.id.commentListView);
		commentTxt = (EditText) findViewById(R.id.commentTxt);
		commentBtn = (Button) findViewById(R.id.commentBtn);

		commentAdaptor = new ArrayAdapter<String>(this,
				android.R.layout.simple_expandable_list_item_1, commentStrList);
		commentItems.setAdapter(commentAdaptor);

		// commentList = commentFunction.retrieveComments("1");
		//
		// if(!commentList.isEmpty()){
		//
		//
		// for(Comment c : commentList){
		// commentList.add(c);
		//
		// }
		//
		// commentAdaptor.notifyDataSetChanged();
		//
		// }
		String groupMediaID = "1";
		commentList = commentFunction.retrieveComments(groupMediaID);
		if (commentList.isEmpty()) {

		} else {
			commentStrList.clear();
			for (Comment c : commentList) {
				addComment(c);
			}
		}

		UserFunctions userFn = new UserFunctions();
		if (userFn.isUserLoggedIn(getApplicationContext())) {
			Globals.uid = userFn.getUid(getApplicationContext());
			userDetails = userFn.getUser(getApplicationContext());
			username = userDetails.get("name");
			userID = userDetails.get("id");
		}

		commentBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				 String commentStr = commentTxt.getText().toString();
				 String userid = username;
				String groupMediaID = "1"; // TODO to change to actual mediaID
				
				 JSONObject json = commentFunction.addComment(userID,
				 groupMediaID, commentStr);
				 try {
				 if (json.get("success") != null) {
				 int success = json.getInt("success");
				 String msg = json.getString("message");
				 if (success == 1) {
				 Toast.makeText(getApplicationContext(), msg,
				 Toast.LENGTH_SHORT).show();
				 } else {
				 Toast.makeText(getApplicationContext(),
				 "not added", Toast.LENGTH_SHORT).show();
				 }
				 }
				 } catch (JSONException e) {
				 // TODO Auto-generated catch block
				 e.printStackTrace();
				 }

				commentList = commentFunction.retrieveComments(groupMediaID);
				if (commentList.isEmpty()) {

				} else {
					commentStrList.clear();
					for (Comment c : commentList) {
						addComment(c);
					}
				}

			}
		});

	}

	private void addComment(Comment comment) {
		String username = comment.getUsername();
		String commentStr = comment.getComment();
		commentStrList.add(username + ": " + commentStr);
		commentAdaptor.notifyDataSetChanged();
		commentTxt.setText(" ");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		// getMenuInflater().inflate(R.menu.draw_comment, menu);
		return true;
	}
}
